(function(){
  //---------------------------
  //Test harness implementation
  //---------------------------  
  var unit_tests = [];
  var tests_executed = 0; 
  var assertions = 0;
  var successes = 0;
  var failures = 0;
  var warnings = 0;
  var executing = false;
  
  //print status message
  window.status_message = function(message) {
    var inner = document.getElementById("resultlist").innerHTML;
    inner = inner+"<li class='status'>"+message+"</li>";
    document.getElementById("resultlist").innerHTML = inner;
  };
  
  //print failure
  window.fail = function(message) {
    assertions=assertions+1;
    failures=failures+1;
    var inner = document.getElementById("resultlist").innerHTML;
    inner = inner+"<li class='failure'>"+message+"</li>";
    document.getElementById("resultlist").innerHTML = inner;
  };
  
  //print warning
  window.warn = function(message) {
    warnings=warnings+1;
    var inner = document.getElementById("resultlist").innerHTML;
    inner = inner+"<li class='warn'>"+message+"</li>";
    document.getElementById("resultlist").innerHTML = inner;
  };
  
  //print success
  window.success = function(message) {
    assertions++;
    successes++;
    var inner = document.getElementById("resultlist").innerHTML;
    inner = inner+"<li class='success'>"+message+"</li>";
    document.getElementById("resultlist").innerHTML = inner;
  };
  
  //assert a test condition, and display feedback
  window.assert = function(value,description) {    
    if (value) {
      success(description);
    }
    else {
      fail("Assertion Failed: "+description);
    }
  };
  
  //end current test
  window.done = function() {
    executing = false;
  };
  
  //print summary
  window.end = function() {
    var inner = document.getElementById("results").innerHTML;
    var sum = "<br/><h3 class='summary'>Test Suite Summary:</h3>"+
      "<div class='summary'>"+
      tests_executed+ " tests ("+
      assertions+" assertions) completed with <span class='success'>"+
      successes+" successes</span>, <span class='warn'>"+ 
      warnings+" warnings</span>, and <span class='failure'>"+
      failures+" failures</span>. <br/><br/>  Have a nice day!</div><br/>";
    document.getElementById("results").innerHTML = sum+inner+sum;
  };
  
  //register an array of unit tests
  window.registerTests = function(tests) {
    unit_tests = unit_tests.concat(tests);
  };
  
  //Start running through tests
  var timer = setInterval(function() {
    if (unit_tests.length == 0 && !executing) {
      clearInterval(timer);
      end();
    }
    if (!executing && unit_tests.length > 0) {
      executing = true;
      var tst = unit_tests.shift();
      status_message("Executing test: '"+ tst.name +"'...");
      tst.unit_test.call({});
      tests_executed++;
    }
  },333);
  
})();